<?php

class CanceledorderController extends BaseController {
//canceled oerder
		public function getCanceledorder()
	{	
		Session::forget('list');
		Session::forget('filter');
		Session::forget('filterby');
		Session::forget('keyword');
		Session::forget('dateby');
		Session::forget('datestart');
		Session::forget('dateend');
		$canceledorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Cancel')
						->paginate(5);
		return View::make('admin.order.canceled.canceledorder')->with('canceledorder',$canceledorder);
	}

	public function postCanceledorder()
	{	

		if(Input::get('filter')=='date'){
			Session::forget('list');
			Session::forget('filterby');
			Session::forget('keyword');
			Session::put('filter','date');
				if(Input::get('datestart')==""){
					return Redirect::back()->with('error', "<font color='red'> Harap masukan tanggal</font><br>");
				}
				if(Input::get('dateend')==""){
					return Redirect::back()->with('error', "<font color='red'> Harap masukan tanggal</font><br>");
				}
			Session::put('datestart',Input::get('datestart'));
			Session::put('dateend',Input::get('dateend'));
			Session::put('dateby',Input::get('dateby'));

			if(Input::get('dateby')=='all'){
			$canceledorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Cancel')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}else if(Input::get('dateby')=='dateorder'){
			$canceledorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Cancel')
						->where('order_status_type','=','Pending')
						->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}else if(Input::get('dateby')=='ordercanceled'){
			$canceledorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Cancel')
						->where('order_status_type','=','Cancel')
						->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Input::get('datestart'))), date("Y-m-d h:m:s",strtotime(Input::get('dateend')))))
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}
			return View::make('admin.order.canceled.canceledorder')->with('canceledorder',$canceledorder);
		}else if(Input::get('filter')=='type'){
			Session::forget('filterby');
			Session::forget('keyword');
			Session::forget('dateby');
			Session::put('filter','type');
			Session::put('list', Input::get('typeby'));
			if(Input::get('typeby')=='all'){
			$canceledorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Cancel')
						->groupBy('order_id')
						->orderBy('order_date','DESC')
						->paginate(5);
			}else{
			$canceledorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Cancel')
						->where('order_name','=',Input::get('typeby'))
						->paginate(5);
			}
			return View::make('admin.order.canceled.canceledorder')->with('canceledorder',$canceledorder);
		}else if(Input::get('filter')=='keyword'){
			Session::put('filter','keyword');
			Session::forget('list');
			Session::forget('dateby');
			Session::put('filterby',Input::get('filterby'));
			Session::put('keyword',Input::get('keyword'));
			if(Input::get('filterby')=='code'){
			$canceledorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Cancel')
						->where('order_code','LIKE','%'.Input::get('keyword').'%')
						->paginate(5);
			return View::make('admin.order.canceled.canceledorder')->with('canceledorder',$canceledorder);
		}else if(Input::get('filterby')=='name'){
			$canceledorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_status','order_status_order_id','=','order_id')
						->join('sys_invoice','invoice_order_id','=','order_id')
						->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
						->where('order_status','=','Cancel')
						->where('customer_name','LIKE','%'.Input::get('keyword').'%')
						->paginate(5);
			return View::make('admin.order.canceled.canceledorder')->with('canceledorder',$canceledorder);
		}
		}else{
        	if(Session::get('filter')=="date"){
        			if(Session::get('dateby')=='all'){
					$canceledorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Cancel')
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Session::get('dateby')=='dateorder'){
					$canceledorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Cancel')
								->where('order_status_type','=','Pending')
								->whereBetween('order_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}else if(Session::get('dateby')=='ordercanceled'){
					$canceledorder = DB::table('sys_order')
								->join('sys_customer','customer_id','=','order_customer_id')
								->join('sys_order_status','order_status_order_id','=','order_id')
								->join('sys_invoice','invoice_order_id','=','order_id')
								->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
								->where('order_status','=','Cancel')
								->where('order_status_type','=','Cancel')
								->whereBetween('order_status_date', array(date("Y-m-d h:m:s",strtotime(Session::get('datestart'))), date("Y-m-d h:m:s",strtotime(Session::get('dateend')))))
								->groupBy('order_id')
								->orderBy('order_date','DESC')
								->paginate(5);
					}
        	}else if(Session::get('filter')=="type"){
					if(Session::get('list')=='all'){
					$canceledorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','Cancel')
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}else{
					$canceledorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','Cancel')
									->where('order_name','=',Session::get('list'))
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}
        	}else if(Session::get('filter')=="keyword"){
        			if(Session::get('filterby')=='code'){
						$canceledorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','Cancel')
									->where('order_code','LIKE','%'.Session::get('keyword').'%')
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}else if(Session::get('filterby')=='name'){
						$canceledorder = DB::table('sys_order')
									->join('sys_customer','customer_id','=','order_customer_id')
									->join('sys_order_status','order_status_order_id','=','order_id')
									->join('sys_invoice','invoice_order_id','=','order_id')
									->leftjoin('sys_invoice_detail','invoice_detail_invoice_id','=','invoice_id')
									->where('order_status','=','Cancel')
									->where('customer_name','LIKE','%'.Session::get('keyword').'%')
									->groupBy('order_id')
									->orderBy('order_date','DESC')
									->paginate(5);
					}
			}
		}
		return View::make('admin.order.canceled.canceledorder')->with('canceledorder',$canceledorder);
		



	}

	public function getDetailcanceledorder($id)
	{	
		$canceledorder = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_idcard','order_idcard_order_id','=','order_id')
						->join('ref_idcard_type','order_idcard_idcard_type_id','=','idcard_type_id')
						->join('ref_idcard_quality','order_idcard_idcard_quality_id','=','idcard_quality_id')
						->where('order_status','=','Cancel')
						->where('order_id','=',$id)
						->first();
		$detailorder =  DB::table('sys_order')
						->join('sys_order_idcard','order_idcard_order_id','=','order_id')
						->join('sys_order_idcard_detail','order_idcard_detail_order_idcard_id','=','order_idcard_id')
						->join('ref_additional_features','order_idcard_detail_additional_features_id','=','additional__features_id')
						->where('order_status','=','Cancel')
						->where('order_id','=',$id)
						->get();
		return View::make('admin.order.canceled.detailcanceledorder', compact('canceledorder',$canceledorder,'detailorder',$detailorder));
	}

	public function postCanceledordercancel()
	{
		$orders = DB::table('sys_order')
				->get();
		$a = 0;
		foreach($orders as $order){
		if(Input::get($order->order_id)=='1'){
			$a = $a + Input::get($order->order_id);
			$orders = DB::table('sys_order')
					->where('order_id','=',$order->order_id)
					->update(array('order_status' => 'Pending'));

			$orderstatus = New orderstatus;
			$orderstatus->order_status_order_id = $order->order_id;
			$orderstatus->order_status_type = 'Pending';
			$orderstatus->order_status_date = date('Y-m-d H:i:s');
			$orderstatus->order_status_log_admin_id = Session::get('admin_id');
			$orderstatus->save();

			$log = New adminlog();
			$log->admin_log_admin_id = Session::get('admin_id');	
			$log->admin_log_activity = "Set cancel order : ".$order->order_id." Pending";	
			$log->admin_log_date = date('Y-m-d H:i:s');	
			$log->save();
		}
		}
		if($a<>0){
		return Redirect::to('/order/canceledorder')->with('message','Order telah dicancel dari daftar approve');
		}
		return Redirect::to('/order/canceledorder')->with('error','Silahkan pilih order');

	}


}